package com.yyhouc.getway.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * 动态路由组件
 */
@Component
public class NacosRoute {

    private String dataId = "getwayRoutes";

    private String group = "DEFAULT_GROUP";

    @Value("${spring.cloud.nacos.config.server-addr}")
    private String serverAddr;
    @Value("${spring.cloud.nacos.config.namespace:-1}")
    private String namespace;
    @Autowired
    private NacosRouteService nacosRouteService;

    @PostConstruct
    public void init() {
        try{
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            if(!"-1".equals(namespace)){
                properties.put("namespace", namespace);
            }
            //从工厂方法获取ConfigService
            ConfigService configService = NacosFactory.createConfigService(properties);
            if(configService == null){
                return;
            }
            String configInfo = configService.getConfig(dataId, group,5000);
            List<RouteDefinition> definitionList = JSON.parseArray(configInfo, RouteDefinition.class);
            for(RouteDefinition definition : definitionList){
                nacosRouteService.addRoute(definition);
            }
        } catch (Exception e) {
        }
        routeByNacosListener();
    }

    public void routeByNacosListener() {
        try {
            Properties properties = new Properties();
            properties.put("serverAddr", serverAddr);
            if(!"-1".equals(namespace)){
                properties.put("namespace", namespace);
            }
            //从工厂方法获取ConfigService
            ConfigService configService = NacosFactory.createConfigService(properties);
            configService.addListener(dataId, group, new Listener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    nacosRouteService.clearRoute();
                    try {
                        List<RouteDefinition> gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class);
                        for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
                            nacosRouteService.addRoute(routeDefinition);
                        }
                        nacosRouteService.publish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public Executor getExecutor() {
                    return null;
                }
            });
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
}